package com.ruoyi.announcement.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.announcement.mapper.AnnouncementMapper;
import com.ruoyi.announcement.domain.Announcement;
import com.ruoyi.announcement.service.IAnnouncementService;

import javax.xml.crypto.Data;

/**
 * 公告Service业务层处理
 * 
 * @author xianyu
 * @date 2025-02-07
 */
@Service
public class AnnouncementServiceImpl implements IAnnouncementService 
{
    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 查询公告
     * 
     * @param noticeId 公告主键
     * @return 公告
     */
    @Override
    public Announcement selectAnnouncementByNoticeId(Long noticeId)
    {
        return announcementMapper.selectAnnouncementByNoticeId(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param announcement 公告
     * @return 公告
     */
    @Override
    public List<Announcement> selectAnnouncementList(Announcement announcement)
    {
        return announcementMapper.selectAnnouncementList(announcement);
    }

    /**
     * 新增公告
     * 
     * @param announcement 公告
     * @return 结果
     */
    @Override
    public int insertAnnouncement(Announcement announcement)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            Long userId = loginUser.getUser().getUserId();
            announcement.setPublisherId(userId);
        }
        Date now = new Date();
        announcement.setCreatedAt(now);
        return announcementMapper.insertAnnouncement(announcement);
    }

    /**
     * 修改公告
     * 
     * @param announcement 公告
     * @return 结果
     */
    @Override
    public int updateAnnouncement(Announcement announcement)
    {
        Date now = new Date();
        announcement.setUpdatedAt(now);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            Long userId = loginUser.getUser().getUserId();
            announcement.setPublisherId(userId);
        }
        return announcementMapper.updateAnnouncement(announcement);
    }

    /**
     * 批量删除公告
     * 
     * @param noticeIds 需要删除的公告主键
     * @return 结果
     */
    @Override
    public int deleteAnnouncementByNoticeIds(Long[] noticeIds)
    {
        return announcementMapper.deleteAnnouncementByNoticeIds(noticeIds);
    }

    /**
     * 删除公告信息
     * 
     * @param noticeId 公告主键
     * @return 结果
     */
    @Override
    public int deleteAnnouncementByNoticeId(Long noticeId)
    {
        return announcementMapper.deleteAnnouncementByNoticeId(noticeId);
    }
}
